var express = require('express');
var router = express.Router();
const multiparty = require('multiparty')
const { userModel, merchantModel, goodsModel,breedModel} = require('../model/lsj_model')

router.get("/getdata", async (req, res) => {
  res.send("获取成就")
})
// 获取用户
router.get("/getuser", async (req, res) => {
  let namekey = req.query.name
  let sexkey = req.query.sex
  let user_names = new RegExp(namekey, 'i') // 不分区大小写
  let user_sex = new RegExp(sexkey, 'i') // 不分区大小写
  let data
  if (user_names != '' || user_sex != '') {
    data = await userModel.find({ $or: [{ name: { $regex: user_names }, sex: { $regex: user_sex } }] })
  } else {
    data = await userModel.find({})
  }

  res.json({ msg: 'ok', data })
})
// 添加用户
router.post('/adduser', async (req, res) => {
  user = await userModel.create(req.body)
  console.log(user, '===user');
  res.json({ msg: 'ok', code: '200', user })
})

// 删除用户
router.delete("/deluser", async (req, res) => {
  let id = req.query.id
  await userModel.deleteOne({ _id: id })
  res.send({ msg: 'ok', code: 200 })
})

// 修改用户
router.patch('/uppuser', async (req, res) => {
  let uid = req.query.id
  let user = await userModel.updateOne({ _id: uid }, req.body)
  res.json({ msg: 'ok', code: 200, user })
})



// 获取店家 及店下所有商品
router.get("/getmerchant_goods", async (req, res) => {
  let data = await merchantModel.aggregate([
    {
      $lookup: {
        from: 'goods',
        localField: '_id',
        foreignField: 'merchant_id',
        as: 'merchant'
      }
    }
  ])
  res.json({ msg: 'ok', data })
})



// 添加店家 商家
router.post('/addmerchant', async (req, res) => {
  data = await merchantModel.create(req.body)
  res.json({ msg: 'ok', code: '200', data })
})


// 获取店家 商家
router.get('/getmerchant', async (req, res) => {
  let namekey = req.query.name
  let addresskey = req.query.address
  let names = new RegExp(namekey, 'i') // 不分区大小写
  let address_s = new RegExp(addresskey, 'i') // 不分区大小写
  let data
  if (names != '' || address_s != '') {
    data = await merchantModel.find({ $or: [{ merchant_name: { $regex: names }, merchant_address: { $regex: address_s } }] })
  } else {
    data = await merchantModel.find()
  }
  res.json({ msg: 'ok', code: '200', data })
})


// 删除店家 商家
router.delete('/delmerchant', async (req, res) => {
  let did = req.query.id
  await merchantModel.deleteOne({ _id: did })
  res.json({ msg: 'ok', code: 200, })
})



// 修改店家
router.patch('/uppmerchant', async (req, res) => {
  let uid = req.query.id
  let user = await merchantModel.updateOne({ _id: uid }, req.body)
  res.json({ msg: 'ok', code: 200, user })
})



// 上传图片
router.post('/upload', async (req, res) => {
  var form = new multiparty.Form();
  form.uploadDir = 'upload'
  form.parse(req, function (err, fields, files) {
    let path = files.file[0].path
    res.json({ msg: 'ok', code: 200, path })
  });
})



// 获取宠物
router.get("/getbreed", async (req, res) => {
  let data = await breedModel.find({}).exec()
  res.json({ msg: 'ok', data })
})





module.exports = router;